package com.qeegoo.b2bautozityp.moudle.login.viewmodel;

import android.databinding.ObservableField;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.api.RequestApi;

import java.util.Random;

import base.lib.util.ToastUtils;

/**
 * Created by lining on 2017/9/19.
 */

public class PhoneValidateViewModel {


    private RequestApi mRequestApi;
    public ObservableField<String> icCode = new ObservableField<>();
    public ObservableField<String> icCodeUri = new ObservableField<>();
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> smsCode = new ObservableField<>();
    public ObservableField<String> key = new ObservableField<>();

    public PhoneValidateViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
    }

    //清除电话号码
    public ReplyCommand clearCommand = new ReplyCommand(() -> {
        phone.set("");
    });

    //获取图片验证码
    public ReplyCommand iconCommand = new ReplyCommand(() -> {
        ToastUtils.showToast("获取图片验证码");
    });

    //获取短信验证码
    public ReplyCommand smsCommand = new ReplyCommand(() -> {

        ToastUtils.showToast("获取短信验证码");
    });

    //下一步
    public ReplyCommand saveCommand = new ReplyCommand(() -> {
        ToastUtils.showToast("下一步");
    });

    public void loadPhoneCode() {

    }

    public void loadCaptcha() {
        getRandomString();
        icCodeUri.set("http://ccj.autozi.com/api/common/captcha?key="+key.get());
    }

    public void getRandomString() { //length表示生成字符串的长度10-20

        int max=20;
        int min=10;
        Random randomLen = new Random();

        int length = randomLen.nextInt(max)%(max) + min;
        String base = "abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        key.set(sb.toString());
//        return key.get();
    }
}
